package clientGui;

import javax.swing.text.*;
import javax.swing.JTextField;

/**
 * @author Wojciech Łopata
 *
 */

public class PortField extends JTextField
{
	private static final long serialVersionUID = -695483663733494411L;

	public PortField()
	{
		super(5);
	}
	
	public int getPort()
	{
		return Integer.valueOf(getText());
	}

	@Override
	protected Document createDefaultModel() 
	{
		return new PortFieldDocument(this);
	}

	private static class PortFieldDocument extends PlainDocument
	{
		private static final long serialVersionUID = 8204877070791736963L;
		private PortField portField;
		
		public PortFieldDocument(PortField portField)
		{
			super();
			this.portField = portField;
		}
		
		public void insertString(int offs, String str, AttributeSet a) 
		throws BadLocationException
		{
			if(str == null)	return;
			if(str.length() + portField.getText().length() > 5) return;
			if(str.matches("[^0-9]+")) return;
			super.insertString(offs, str, a);
		}
	}
}